package com.yanqu.road.server.timer.bug;

import com.yanqu.road.entity.config.building.BuildingInfo;
import com.yanqu.road.entity.config.building.BuildingUpgrade;
import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.enums.eRankType;
import com.yanqu.road.entity.player.UserBuilding;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.player.UserBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.*;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.config.BuildingMgr;
import com.yanqu.road.server.manger.config.HouseMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

public class BugTask extends GeneralTask {

    protected Logger logger = LogManager.getLogger(BugTask.class.getName());

    private UserInfo userInfo;

    public BugTask(UserInfo uInfo) {
        this.userInfo = uInfo;
    }

    @Override
    public void doRun() {
        GamePlayer player = new GamePlayer(this.userInfo);
        BuildingModule buildingModule = player.getModule(BuildingModule.class);
        CardModule cardModule = player.getModule(CardModule.class);
        PlayerModule playerModule = player.getModule(PlayerModule.class);
        PatronsModule patronsModule = player.getModule(PatronsModule.class);
        BeautyModule beautyModule = player.getModule(BeautyModule.class);
        EstateWarModule estateWarModule = player.getModule(EstateWarModule.class);

        buildingModule.loadData();
        cardModule.loadData();
        playerModule.setUserInfo(this.userInfo);
        patronsModule.loadData();
        beautyModule.loadData();
        estateWarModule.loadData();

        calEarnSpeed(player);
        UserMgr.setUserInfo(player.getUserInfo());
        UserBussiness.updateBugGoldSpeed(userInfo);
        logger.info("cal user : {}", player.getUserInfo().getUserId());
    }

    public static void calEarnSpeed(GamePlayer player) {
        //店铺赚速
        BigDecimal buildEarnSpeed = new BigDecimal(calEarnSpeedInModule(player, player.getModule(BuildingModule.class).getBuildingMap()));
        //子嗣赚速
        BigDecimal childEarnSpeed = new BigDecimal(player.getUserInfo().getChildEarnSpeed());

        BigDecimal baseEarnSpeed = buildEarnSpeed.add(childEarnSpeed);
        BigInteger tempBaseEarnSpeed = baseEarnSpeed.toBigInteger();
        if (tempBaseEarnSpeed.compareTo(player.getUserInfo().getBaseEarnSpeed()) != 0) {//基础赚速变化
            player.getUserInfo().setBaseEarnSpeed(tempBaseEarnSpeed);
        }
        //加成
        BigDecimal addition = BigDecimal.ONE;
        //特权卡加成
        addition = addition.add(BigDecimal.valueOf(((double) player.getModule(CardModule.class).getEarnSpeedAddition()) / 1000).setScale(4, BigDecimal.ROUND_UP));
        //点赞、蛊惑加成
        addition = addition.add(BigDecimal.valueOf(((double) player.getUserInfo().getHeartCount() * GameConfig.HEART_ADDITION_EFFECT) / 1000).setScale(4, BigDecimal.ROUND_UP));//加上子嗣赚速

        BigInteger newEarnSpeed = baseEarnSpeed.multiply(addition).setScale(0, BigDecimal.ROUND_UP).toBigInteger();
        if (!newEarnSpeed.equals(BigInteger.ZERO)) {//赚速变化
            player.getModule(PlayerModule.class).getUserInfo().setEarnSpeed(newEarnSpeed);
            //赚速榜
            RankMgr.changeUserRank(eBigRankType.Normal.getValue(), player.getUserId(), newEarnSpeed, eRankType.EarnSpeed.getValue(), "");
            //家族赚速变化
            CrossUnionMgr.updateMemberEarnSpeed(player);
        }

    }


    public static BigInteger calEarnSpeedInModule(GamePlayer player, Map<Integer, UserBuilding> buildingMap) {
        BigInteger totalEarnSpeed = BigInteger.ZERO;
        Map<Integer, UserBuilding> copyBuildingMap = new HashMap<>(buildingMap);
        // 计算店铺赚速
        for (UserBuilding building : copyBuildingMap.values()) {
            building.setBuildingEarnSpeed(BuildingMgr.getBuildingEarnSpeed(player, building).toBigInteger());
            totalEarnSpeed = totalEarnSpeed.add(building.getBuildingEarnSpeed());
        }
        return totalEarnSpeed;
    }

    public static BigDecimal getBuildingEarnSpeed(GamePlayer player, UserBuilding userBuilding) {
        if (null == userBuilding) {
            return BigDecimal.ZERO;
        }
        BigDecimal earnSpeed;
        //单伙计赚速
        BigDecimal h = BigDecimal.ZERO;
        BuildingInfo buildingInfo = BuildingMgr.getBuildingInfo(userBuilding.getBuildingId());
        if (null != buildingInfo) {
            h = buildingInfo.getStaffEarnSpeed();
        }
        //伙计数量
        BigDecimal n = BigDecimal.valueOf(userBuilding.getStaffCount());
        //第一次计算
        BigDecimal value1 = h.multiply(n);

        //门客赚速
        BigDecimal z = new BigDecimal(player.getModule(PatronsModule.class).getTotalPatronsAbility());

        //第二次计算
        BigDecimal value2 = z.divide(BigDecimal.valueOf(ConfigMgr.getPatronsAbilityParam5()));
        value2 = value2.add(value1);

        //初步加成
        BigDecimal addition = BigDecimal.ONE;
        //升级店铺的加成
        BuildingUpgrade buildingUpgrade = BuildingMgr.getBuildingUpgrade(userBuilding.getBuildingId(), userBuilding.getLevel());
        if (null != buildingUpgrade) {
            addition = BigDecimal.valueOf((double) buildingUpgrade.getEarnSpeedAddition() / 1000).setScale(4, BigDecimal.ROUND_UP);
        }


        //美女对店铺加成
        addition = addition.add(BigDecimal.valueOf(BuildingMgr.getBeautyEarnSpeedAddition(player, userBuilding) / 1000).setScale(4, BigDecimal.ROUND_UP));

        //派遣门客的加成
        addition = addition.add(BigDecimal.valueOf(BuildingMgr.getDispatchPatronsAddition(player, userBuilding) / 1000).setScale(4, BigDecimal.ROUND_UP));

        //乔迁风云府邸赚速加成
        addition = addition.add(BigDecimal.valueOf(((double) player.getModule(EstateWarModule.class).getEarnSpeedAddition()) / 1000).setScale(4, BigDecimal.ROUND_UP));

        //钱庄赚速加成
        addition = addition.add(BigDecimal.valueOf(((double) HouseMgr.getHouseInfo(player.getUserInfo().getHouseLv()).getEarnSpeedAddition()) / 1000).setScale(4, BigDecimal.ROUND_UP));

        //商会议事厅赚速加成
        addition = addition.add(BigDecimal.valueOf(BuildingMgr.getUnionChamberDispatchAddition(player, userBuilding) / 1000).setScale(4, BigDecimal.ROUND_UP));

        //最后计算
        earnSpeed = value2.multiply(addition).setScale(0, BigDecimal.ROUND_UP);

        return earnSpeed;
    }


}
